package com.szgr.baoyin.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.szgr.baoyin.R;
import com.szgr.baoyin.adapter.CommonFragmentPagerAdapter;
import com.szgr.baoyin.fragment.OrderAllFragment;
import com.szgr.baoyin.fragment.OrderToGetFragment;
import com.szgr.baoyin.fragment.OrderToLeaveFragment;
import com.szgr.baoyin.fragment.OrderToPayFragment;
import com.szgr.baoyin.fragment.OrderToSendFragment;

import java.util.ArrayList;
import java.util.List;

public class MyOrderActivity extends BaseActivity implements ViewPager.OnPageChangeListener, RadioGroup.OnCheckedChangeListener {
    private List<Fragment> fragments;
    private ViewPager pager;
    private RadioGroup radioGroup;
//    private RadioButton rb_all;
//    private RadioButton rb_pay;
//    private RadioButton rb_send;
//    private RadioButton rb_get;
//    private RadioButton rb_leave;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_my_order);
        super.onCreate(savedInstanceState);
    }

    @Override
    public void initActionBar() {
        actionBarTitle.setText("我的订单");
    }

    @Override
    public void initView() {
        fragments = new ArrayList<>();
        fragments.add(new OrderAllFragment());
        fragments.add(new OrderToPayFragment());
        fragments.add(new OrderToSendFragment());
        fragments.add(new OrderToGetFragment());
        fragments.add(new OrderToLeaveFragment());
        pager = (ViewPager) findViewById(R.id.order_view_pager);
        pager.setAdapter(new CommonFragmentPagerAdapter(getSupportFragmentManager(), fragments));
        radioGroup = (RadioGroup) findViewById(R.id.order_radio_group);

//        rb_all = (RadioButton) findViewById(R.id.order_all);
//        rb_pay = (RadioButton) findViewById(R.id.order_pay);
//        rb_send = (RadioButton) findViewById(R.id.order_send);
//        rb_get = (RadioButton) findViewById(R.id.order_get);
//        rb_leave = (RadioButton) findViewById(R.id.order_leave);

        radioGroup.setOnCheckedChangeListener(this);
        pager.setOnPageChangeListener(this);

        Intent intent = getIntent();
        if(intent != null && intent.hasExtra("pos")){
            int pos = intent.getIntExtra("pos", 0);
            RadioButton childAt = (RadioButton) radioGroup.getChildAt(pos);
            childAt.setChecked(true);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.action_bar_left:
                finish();
                break;
        }
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        RadioButton childAt = (RadioButton) radioGroup.getChildAt(position);
        childAt.setChecked(true);
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        int childCount = group.getChildCount();
        for (int i = 0; i < childCount; i++) {
            RadioButton childAt = (RadioButton) group.getChildAt(i);
            childAt.setTextColor(Color.BLACK);
            if(childAt.isChecked()){
                pager.setCurrentItem(i);
                childAt.setTextColor(0xfff08200);
            }
        }
    }
}
